/*
 *  Jiki - a small lightweight wiki engine.
 *
 *  Copyright 2008 Peter Halbmayer <phalbmayer@gmail.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

package at.pmsa.jiki.conn.http;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import at.pmsa.jiki.util.IJConstants;

public class HTTPResponse implements IJConstants{

	private String protocol;

	private String version;

	private int code;

	private String status;

	private String date;

	private String connection;

	private String contentType;

	private StringBuffer content;

	public HTTPResponse(int code){
		this(code, null);
	}

	public HTTPResponse(int code, StringBuffer content){
		
		this.code = code;
		if(content == null)
			this.content = new StringBuffer();
		else
			this.content = content;
		
		switch(code){
			case HTTP_OK:
				status = OK;
				break;
			case HTTP_NOT_FOUND:
				status = NOT_FOUND;
				break;
			case HTTP_SERVER_ERROR:
				status = INTERNAL_SERVER_ERROR;
				break;
			case HTTP_BAD_METHOD:
				status = BAD_METHOD;
				break;
		}
		
		if(status == null){
			throw new IllegalArgumentException("Illegal status code: " + code);
		}
		
		date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US).format(new Date());
		protocol = "HTTP";
		version = "1.1";
		connection = "close";
		setContentType("text/html; charset=utf-8");
	}
	
	void resolveMimeType(String filename){
		if(filename.endsWith(".html") || filename.endsWith(".htm")){
			setContentType("text/html; charset=utf-8");
		}
		else if(filename.endsWith(".jpg") || filename.endsWith(".jpeg")){
			setContentType("image/jpeg");
		}
		else if(filename.endsWith(".gif")){
			setContentType("image/gif");
		}
		else if(filename.endsWith(".png")){
			setContentType("image/png");
		}
		else if(filename.endsWith(".css")){
			setContentType("text/css");
		}
		else if(filename.endsWith(".js")){
			setContentType("application/javascript");
		}
		else{
			setContentType("application/unknown");
		}
	}
	
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getContentType() {
		return contentType;
	}
	
	public void addContent(String cont){
		content.append(cont);
	}
	
	public void setContent(String cont){
		content.delete(0, content.length());
		content.append(cont);
	}
	
	public StringBuffer getContent(){
		return content;
	}
	
	public String printRes(){
		StringBuilder builder = new StringBuilder();
		
		builder.append(protocol).append("/").append(version).append(" ").append(code);
		builder.append(" ").append(status).append("\r\n");
		return builder.toString();
	}

	public String toString(){
		StringBuilder builder = new StringBuilder();
		
		builder.append(protocol).append("/").append(version).append(" ").append(code);
		builder.append(" ").append(status).append("\r\n");
		builder.append("Date: ").append(date).append("\r\n");
		builder.append("Server: ").append(SERVER).append("\r\n");
		builder.append("Connection: ").append(connection).append("\r\n");
		
		if( content != null && content.length() > 0){
			builder.append("Content-Length: ").append(content.toString().getBytes().length).append("\r\n");
			builder.append("Content-Type: ").append(getContentType()).append("\r\n");
			builder.append("\r\n").append(content);
			builder.append("\r\n");
		}
		return builder.toString();
	}

//	public String toString(){
//		StringBuilder builder = new StringBuilder();
//		
//		builder.append(protocol).append("/").append(version).append(" ").append(code);
//		builder.append(" ").append(status).append("\r\n");
//		builder.append("Date: ").append(date).append("\r\n");
//		builder.append("Server: ").append(SERVER).append("\r\n");
//		builder.append("Connection: ").append(connection).append("\r\n");
//		
//		if( content != null && content.length() > 0){
//			builder.append("Content-Length: ").append(content.toString().getBytes().length).append("\r\n");
//			builder.append("Content-Type: ").append(getContentType()).append("\r\n");
//			builder.append("\r\n").append(content);
//			builder.append("\r\n");
//		}
//		return builder.toString();
//	}
}
